const db = require('../config/db');

class Friend {
  static addFriend(userId, friendId, callback) {
    // 添加 A -> B 的好友关系
    db.run(
      'INSERT INTO friends (user_id, friend_id) VALUES (?, ?)',
      [userId, friendId],
      (err) => {
        if (err) {
          return callback(err);
        }
        // 添加 B -> A 的好友关系
        db.run(
          'INSERT INTO friends (user_id, friend_id) VALUES (?, ?)',
          [friendId, userId],
          callback
        );
      }
    );
  }

  static getFriends(userId, callback) {
    db.all(
      `SELECT u.id, u.username 
       FROM friends f
       JOIN users u ON f.friend_id = u.id
       WHERE f.user_id = ?`,
      [userId],
      callback
    );
  }

  static areFriends(userId, friendId, callback) {
    db.get(
      `SELECT * FROM friends 
       WHERE (user_id = ? AND friend_id = ?)
       OR (user_id = ? AND friend_id = ?)`,
      [userId, friendId, friendId, userId],
      callback
    );
  }
}

module.exports = Friend; 